//
//  WLToastView.m
//  VPNLinker
//
//  Created by 吴磊 on 2025/5/15.
//

#import "WLToastView.h"

@implementation WLToastView
+ (void)showToast:(NSString *)message {
    [self showToast:message duration:2.0 position:ToastPositionBottom style:nil];
}

+ (void)showToast:(NSString *)message duration:(NSTimeInterval)duration {
    [self showToast:message duration:duration position:ToastPositionBottom style:nil];
}

+ (void)showToast:(NSString *)message duration:(NSTimeInterval)duration position:(ToastPosition)position {
    [self showToast:message duration:duration position:position style:nil];
}

+ (void)showToast:(NSString *)message duration:(NSTimeInterval)duration position:(ToastPosition)position style:(NSDictionary *)style {
    // 确保在主线程执行
    dispatch_async(dispatch_get_main_queue(), ^{
        // 获取keyWindow
        UIWindow *window = [UIApplication sharedApplication].keyWindow;
        if (!window) {
            window = [[UIApplication sharedApplication].windows firstObject];
        }
        
        // 创建容器视图
        UIView *toastView = [[UIView alloc] init];
        toastView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.7];
        toastView.layer.cornerRadius = 10;
        toastView.layer.masksToBounds = YES;
        toastView.alpha = 0;
        
        // 创建消息标签
        UILabel *messageLabel = [[UILabel alloc] init];
        messageLabel.text = message;
        messageLabel.textColor = [UIColor whiteColor];
        messageLabel.font = [UIFont systemFontOfSize:14];
        messageLabel.textAlignment = NSTextAlignmentCenter;
        messageLabel.numberOfLines = 0;
        
        // 应用自定义样式
        if (style) {
            if (style[@"backgroundColor"]) {
                toastView.backgroundColor = style[@"backgroundColor"];
            }
            if (style[@"textColor"]) {
                messageLabel.textColor = style[@"textColor"];
            }
            if (style[@"font"]) {
                messageLabel.font = style[@"font"];
            }
            if (style[@"cornerRadius"]) {
                toastView.layer.cornerRadius = [style[@"cornerRadius"] floatValue];
            }
        }
        
        // 计算标签大小
        CGSize maxSize = CGSizeMake(window.bounds.size.width * 0.7, window.bounds.size.height * 0.7);
        CGSize labelSize = [message boundingRectWithSize:maxSize
                                                options:NSStringDrawingUsesLineFragmentOrigin
                                             attributes:@{NSFontAttributeName: messageLabel.font}
                                                context:nil].size;
        
        // 设置视图frame
        CGFloat padding = 15;
        toastView.frame = CGRectMake(0, 0, labelSize.width + padding * 2, labelSize.height + padding * 2);
        messageLabel.frame = CGRectMake(padding, padding, labelSize.width, labelSize.height);
        
        [toastView addSubview:messageLabel];
        [window addSubview:toastView];
        
        // 设置位置
        CGFloat centerX = window.bounds.size.width / 2;
        CGFloat centerY = 0;
        
        switch (position) {
            case ToastPositionTop:
                centerY = window.safeAreaInsets.top + 100;
                break;
            case ToastPositionCenter:
                centerY = window.bounds.size.height / 2;
                break;
            case ToastPositionBottom:
                centerY = window.bounds.size.height - window.safeAreaInsets.bottom - 100;
                break;
        }
        
        toastView.center = CGPointMake(centerX, centerY);
        
        // 动画显示
        [UIView animateWithDuration:0.3 animations:^{
            toastView.alpha = 1;
        } completion:^(BOOL finished) {
            // 延迟隐藏
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(duration * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [UIView animateWithDuration:0.3 animations:^{
                    toastView.alpha = 0;
                } completion:^(BOOL finished) {
                    [toastView removeFromSuperview];
                }];
            });
        }];
    });
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
